package com.wy.framework.actions.org;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.developer.developer.services.RegistService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.pojos.TSysOrginfo;
import com.wy.framework.services.org.OrgManageService;
import com.wy.framework.services.user.UserManageService;
import com.wy.component.file.FileUtil;

/**
 * 项目名称：openportal
 * 类名称：OrgAction
 * 类描述：机构信息管理
 * 创建人：张旭星
 * 创建时间：2015-12-28 下午7:35:56
 * 修改人：张旭星
 * 修改时间：2015-12-28 下午7:35:56
 * 版本信息：@version 1.0
 * Copyright (c) 2015万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/manage/org")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class OrgAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(OrgAction.class);
	
	public TSysOrginfo tSysOrginfo;

	@Autowired
	private OrgManageService orgManageService;
	@Autowired
	private RegistService registService;
	
	private List<Map> rows;
	private List<Map> orgInfo;
	private String id;
	private String jsonRes;

	@Autowired
	private UserManageService userManageService;
	
	@Action(value = "addHead", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String addHead()throws ServiceException{
		String dataURL=URLDecoder.decode(ServletActionContext.getRequest().getParameter("dataURL"));
		String source=ServletActionContext.getRequest().getParameter("source");
		String headRootPath=this.getClass().getClassLoader().getResource("/").getPath().replace("/WEB-INF/classes", "");
		String headPathBody="";
		if(StringUtils.equals(source, "orgModify")){
			String org=ServletActionContext.getRequest().getParameter("org");
			List<Map> list=orgManageService.queryParentOrg(org);
			String logoName=(String) list.get(0).get("ORGLOGO");
			if(StringUtils.isNotEmpty(logoName)){
				logoName=logoName.substring(logoName.indexOf("orgImg/")+7,logoName.lastIndexOf(".jpg"));
				headPathBody = "/upload/orgImg/"+logoName+".jpg";
			}else
				headPathBody = "/upload/orgImg/"+UUID.randomUUID().toString()+".jpg";
		}else{
			headPathBody = "/upload/orgImg/"+UUID.randomUUID().toString()+".jpg";
		}
	   
		rspCode=userManageService.saveHead(dataURL, headRootPath, headPathBody);
		jsonRes=headRootPath+headPathBody;
		return SUCCESS;
	}
	

	@Action(value = "loadOrgTree", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","orgInfo" }) })
	public String loadOrgTree() throws Exception {
		orgInfo = orgManageService.loadOrgTreeInfo(id,this.loginUser());
		return SUCCESS;
	}

	@Action(value = "loadAllOrg", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","orgInfo" }) })
	public String loadAllOrg() throws ServiceException, IOException,
			ErrorSqlException {
		orgInfo = orgManageService.loadOrgAllInfo();
		return SUCCESS;
	}
	
	@Action(value = "loadParentOrg", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String loadParentOrg()throws ServiceException{
		String orgCode=ServletActionContext.getRequest().getParameter("orgCode");
    	rows=orgManageService.queryParentOrg(orgCode);
		return SUCCESS;
	}
	
	
	@Action(value = "modify", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String modify() throws ServiceException {
			String orgCode = ServletActionContext.getRequest().getParameter("orgCode");
			String orgName = ServletActionContext.getRequest().getParameter("orgName");
			String state = ServletActionContext.getRequest().getParameter("state");
			try {
				rspCode=orgManageService.updateOrgInfo(orgCode,orgName,state);
			} catch (ErrorSqlException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return SUCCESS;
	}
	
	@Action(value = "add", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String add() throws ServiceException {
		String parentOrgCode = ServletActionContext.getRequest().getParameter("parentOrgCode");
		String orgName = ServletActionContext.getRequest().getParameter("orgName");
		String head = ServletActionContext.getRequest().getParameter("head");
		
		try {
			orgManageService.addOrg(head,orgName,parentOrgCode);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@Action(value = "delete", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String delete() throws ServiceException {
		try {
			String orgCode = ServletActionContext.getRequest().getParameter("orgCode");
			rspCode = orgManageService.deleteOrgInfo(orgCode);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public List<Map> getRows() {
		return rows;
	}

	public List<Map> getOrgInfo() {
		return orgInfo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

	public TSysOrginfo gettSysOrginfo() {
		return tSysOrginfo;
	}

	public void settSysOrginfo(TSysOrginfo tSysOrginfo) {
		this.tSysOrginfo = tSysOrginfo;
	}


	public String getJsonRes() {
		return jsonRes;
	}


	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}


	
}
